﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;

namespace CRM.Op.Common
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class CurrentUser
    {
        /// <summary>
        /// 用户id
        /// </summary>
        public long UserId;

        /// <summary>
        /// 公司名称
        /// </summary>
        public string CompanyName;

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName;

        /// <summary>
        /// 公司ID
        /// </summary>
        public int CompanyID { set; get; }

        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleID { set; get; }

        /// <summary>
        /// 自定义序列化
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("UserId:{0},CompanyName:{1},UserName:{2},CompanyID:{3},RoleID:{4}",
                UserId, CompanyName, UserName, CompanyID, RoleID);
        }

        /// <summary>
        /// 自定义解析
        /// </summary>
        /// <param name="str"></param>
        public void Parse(string str)
        {
            string[] usrs = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < usrs.Length; i++)
            {
                string[] kvp = usrs[i].Split(new String[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                if (kvp.Length > 1)
                {
                    switch (kvp[0])
                    {
                        case "UserId":
                            UserId = Convert.ToInt32(kvp[1]);
                            break;
                        case "CompanyName":
                            CompanyName = kvp[1];
                            break;
                        case "UserName":
                            UserName = kvp[1];
                            break;
                        case "CompanyID":
                            CompanyID = Convert.ToInt32(kvp[1]);
                            break;
                        case "RoleID":
                            RoleID = Convert.ToInt32(kvp[1]);
                            break;
                    }
                }

            }
        }
    }
}